#include "Led.h"
#include "TIMER\timer.h"

/***************************************************
*variable define
***************************************************/

unsigned char index;
/***************************************************
*Process
***************************************************/
 
unsigned char P0Buffer;
unsigned char P1Buffer;
unsigned char P2Buffer;
unsigned char temp;
void scan_led(){

		updateBuffer();
		P2 = 0x0f;
		P1 = 0xff;
		P0 = 0xff;
		P1 &= P1Buffer;
		P0 &= P0Buffer;
		P2 = P2Buffer;
		
}


void updateBuffer()	 {
	   index=(index+1)%sizeFloor;
	   P0Buffer=dataP0[index];
	   P1Buffer=dataP1[index];
	   P2Buffer=P2|0x0f;
	   P2Buffer &= ~(0x01<<index);
}


 void clearLedAll(){
 	int i;
 	for(i=0;i< sizeFloor;i++){
		dataP0[i]=0xff;
		dataP1[i]=0xff;
	}
	 
  }
/*	   	Use P0 and P1 for  Column- Row ,P2 for Floor   (Timer 1)
 *		 
 *	3	P0.3	P0.7   	P1.3   	P1.7
 *	^	P0.2	P0.6   	P1.2   	P1.6 
 *	|	P0.1	P0.5   	P1.1   	P1.5
 *	y	P0.0	P0.4	P1.0	P1.4
 *	0	x - >	1		2	 	3
 *
 *
 *
 */


unsigned char clearbit(unsigned int x,unsigned int y,unsigned int z){
	 if(x>3||y>3||z>3||x<0||y<0||z<0){
	 	return 0;
	 }else{
	  	 if(x<2){ //x<2 setbit to P0
		 	temp=(1<<(y+4*x));
			P0Buffer=dataP0[z]|(~(temp));
			if((dataP0[z]|(~(temp)))!=0xff){
				dataP0[z]|=temp;
				return 1;// Bit was set
			}else{
				dataP0[z]|=temp;
				return 0;
			}	
		 }else{	//x>=2 setbit to P1
			temp=(1<<(y+4*(x-2)));
			P1Buffer=dataP1[z]|(~(temp));
			if((dataP1[z]|(~(temp)))!=0xff){
				dataP1[z]|=temp;
				return 1;
			}else{
				dataP1[z]|=temp;
				return 0;
			}
		 }	
	 }

//	 if(x>3||y>3||z>3||x<0||y<0||z<0){
//	 	// 
//	 }else{
//	  	 if(x<2){ //x<2 setbit to P0
//		 	temp=(1<<(y+4*x));
//			dataP0[z]|=temp;	
//		 }else{	//x>=2 setbit to P1
//			temp=(1<<(y+4*(x-2)));
//			dataP1[z]|=temp;
//		 }	
//	 }
}

unsigned char setbit(unsigned int x,unsigned int y,unsigned int z){	
	 if(x>3||y>3||z>3||x<0||y<0||z<0){
	 	return 0; 
	 }else{
	  	 if(x<2){ //x<2 setbit to P0
		 	temp=(1<<(y+4*x));
			P0Buffer=dataP0[z]|(~(temp));
			if((dataP0[z]|(~(temp)))!=0xff){
				dataP0[z]&=~(temp);
				return 0;// Bit was set
			}else{
				dataP0[z]&=~(temp);
				return 1;
			}	
		 }else{	//x>=2 setbit to P1
			temp=(1<<(y+4*(x-2)));
			P1Buffer=dataP1[z]|(~(temp));
			if((dataP1[z]|(~(temp)))!=0xff){
				dataP1[z]&=~(temp);
				return 0;
			}else{
				dataP1[z]&=~(temp);
				return 1;
			}
		 }	
	 }
}

/*
void setcolumn(unsigned int x,unsigned int y){
	
	 if(x>3||y>3||x<0||y<0){
	 	// 
	 }else{
	  	 if(x<3){ //x<=2 setbit to P0
		 	temp=(1<<(y+4*x));
			dataP0[0]&=~(temp);
			dataP0[1]&=~(temp);
			dataP0[2]&=~(temp);
			dataP0[3]&=~(temp);
				
		 }else{	//x>=3 setbit to P1
			temp=(1<<(y+4*(x-2)));
			dataP1[0]&=~(temp);
			dataP1[1]&=~(temp);
			dataP1[2]&=~(temp);
			dataP1[3]&=~(temp);
		 }	
	 }	
}*/

/*
void clearcolumn(unsigned int x,unsigned int y){
	
	 if(x>3||y>3||x<0||y<0){
	 	// 
	 }else{
	  	 if(x<3){ //x<=2 setbit to P0
		 	temp=(1<<(y+4*x));
			dataP0[0]|=temp;
			dataP0[1]|=temp;
			dataP0[2]|=temp;
			dataP0[3]|=temp;	
		 }else{	//x>=3 setbit to P1
			temp=(1<<(y+4*(x-2)));
			dataP1[0]|=temp;
			dataP1[1]|=temp;
			dataP1[2]|=temp;
			dataP1[3]|=temp;
		 }	
	 }
}*/


void setrow(unsigned int y,unsigned int z){
	 	
	 if(z>3||y>3||z<0||y<0){
	 	// 
	 }else{
		setbit(0,y,z);
		setbit(1,y,z);
		setbit(2,y,z);
		setbit(3,y,z);
			
	 }
}
/*
void clearrow(unsigned int y,unsigned int z){	
	int x;
	 if(z>3||y>3||z<0||y<0){
	 	// 
	 }else{
	 	for(x=0;x<=3;x++){
		clearbit(x,y,z);
		 }
			
	 }
} */
/*
void clearfloor(unsigned int z){
	 dataP0[z]=0xff;
	 dataP1[z]=0xff;
} */
void setfloor(unsigned int z){
 	 dataP0[z]=0;
	 dataP1[z]=0;
} 
void setValue_ledBonus(int value){


		unsigned char temp=0;
		if(value>=0&&value<8)
			temp=value<<4;
		P2&=~(0x70);
		P2|=temp;
	
}